package com.ricohgag.Interceptor;

import com.ricohgag.entity.AdminUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ricohgag
 * @date 2018/9/20 1:33
 */
public class CheckAdmin implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws  Exception{
        List<String> uncheckUrls = new ArrayList<>();
        uncheckUrls.add("/admin/toLogin");
        uncheckUrls.add("/admin/loginAdmin");
        String requestUrl = request.getRequestURI();
        System.err.println("null: "+uncheckUrls);
        if(uncheckUrls.contains(requestUrl)){
            return true;
        }
        AdminUser adminUser = (AdminUser)request.getSession().getAttribute("adminUserSession");
        String pathUrl = request.getContextPath();
        if(adminUser == null){
            response.sendRedirect(pathUrl+"/admin/toLogin");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

    }
}
